package com.example.zzl.adapter.HomeFragment;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.View;
import android.widget.ImageView;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.example.zzl.adapter.MyBaseAdapter;
import com.example.zzl.databinding.HomeSpecialInfoItemBinding;
import com.example.zzl.model.bean.SpecialInfo;
import com.example.zzl.utils.specification.VolleySingleton;

import java.util.List;

/**
 * Created by ligg on 2017/2/11.
 */

public class SpecialInfoAdapter extends MyBaseAdapter<SpecialInfo.ListRecommendInfoBean, SpecialInfoAdapter.ViewHolder> {

    public SpecialInfoAdapter(Context context, List<SpecialInfo.ListRecommendInfoBean> list, int resId) {
        super(context, list, resId);
    }

    @Override
    public ViewHolder bindView(View convertView, List<SpecialInfo.ListRecommendInfoBean> list, int position) {
        HomeSpecialInfoItemBinding db = DataBindingUtil.bind(convertView);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.db = db;
        return viewHolder;
    }

    @Override
    public void bindData(ViewHolder viewHolder, List<SpecialInfo.ListRecommendInfoBean> list, int position) {
        SpecialInfo.ListRecommendInfoBean info = list.get(position);
        viewHolder.db.setInfo(info);
        viewHolder.db.executePendingBindings();
        final ImageView iv = viewHolder.db.iv;
        iv.setImageBitmap(null);
        iv.setTag(info.strMainImage);
        VolleySingleton.volleySingleton.getImageLoader().get(info.strMainImage, new ImageLoader.ImageListener() {
            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                if (iv.getTag().equals(response.getRequestUrl())) {
                    iv.setImageBitmap(response.getBitmap());
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
    }

    class ViewHolder extends MyBaseAdapter.BaseHolder {

        public HomeSpecialInfoItemBinding db;
    }
}
